//
//  Material.cpp
//  RayTracer
//
//  Created by 俞云康 on 2/15/16.
//  Copyright © 2016 yushroom. All rights reserved.
//

#include "Params.hpp"
#include "Material.hpp"
#include "MatteMaterial.hpp"
#include "MirrorMaterial.hpp"
#include "GlassMaterial.hpp"
#include "PlasticMaterial.hpp"
#include "MetalMaterial.hpp"

vector<shared_ptr<Material>> Material::materials;

shared_ptr<Material> Material::create(const Params& params) {
    string type = params.getType();
    string name = params.getID();
    string ref = params.getRef();
    if (ref != "") {
        for (auto& m : materials) {
            if (m->name == ref) {
                return m;
            }
        }
        error("ref material(%s) not found\n", ref.c_str());
    }
    shared_ptr<Material> m;
    if (type == "matte") {
        m = MatteMaterial::create(params);
    } else if (type == "mirror") {
        m = MirrorMaterial::create(params);
    } else if (type == "glass") {
        m = GlassMaterial::create(params);
    } else if (type == "plastic") {
        m = PlasticMaterial::create(params);
	} else if (type == "shinyMetal") {
		m = ShinyMetalMaterial::create(params);
	} else {
        error("unknown Material type: %s", type.c_str());
    }

    if (name != "") {
        for (auto& m : materials) {
            if (m->name == name) {
                error("duplicate material name: %s\n", name.c_str());
            }
        }
    }
    m->name = name;
    materials.push_back(m);
    return m;

}
